import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export interface IUserInfo {
  avatar: string
  nickname: string
  organizationId: number
  type: number
  userId: number
  username: string
}

export const useUserinfo = defineStore('userInfo', () => {
  const cacheLoginInfo = localStorage.getItem('login')
  let userInfo
  let userScore: string[] = []
  let roleCodes: string[] = []
  if (cacheLoginInfo) {
    const { info, scopes, attributes } = JSON.parse(cacheLoginInfo)
    userInfo = info
    userScore = scopes
    roleCodes = attributes.roleCodes
  }
  const user = ref(userInfo)
  const score = ref<string[]>(userScore)
  const roles = ref(roleCodes)
  const setUser = ({ info, scopes , attributes }: { info: IUserInfo; scopes: string[]; attributes: { roleCodes: string[] } }) => {
    user.value = info
    score.value = scopes
    roles.value = attributes.roleCodes
  }
  return {
    user,
    setUser,
    score,
    roles,
  }
})
